//初始化表格
var initGrid = function(){
	$('#riskWarningTable').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
        "bServerSide" : true, //是否启动服务器端数据导入
        "bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
        "bJQueryUI" : true, //是否使用 jQury的UI theme
        "aLengthMenu" : [5, 10, 15], //更改显示记录数选项
        "iDisplayLength" : 10, //默认显示的记录数
        "bAutoWidth" : true, //是否自适应宽度 update\
        "bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
        "bPaginate" : true, //是否显示（应用）分页器
        "bInfo" : true, //是否显示页脚信息，DataTables插件左下角显示记录数
        "sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
        "bSort" : true, //是否启动各个字段的排序功能
        "aaSorting" : [[9, "ASC"]], //默认的排序方式，第3列，升序排列
        "bFilter" : false, //是否启动过滤、搜索功能
        "sScrollX": "100%",
		"sScrollXInner": 1695,
	    "sAjaxSource":"sys/AfterLoanMap/loanCollectionList",
        "fnServerData":retrieveData,  //自定义数据获取函数
        
		"aoColumns" : [ {
			"mDataProp" : "loanCode",
			"sTitle" : "贷款申请号",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return '<a href="javascript:void(0);" onclick="loan_query('+row.loanId+','+row.clientId+');" class="tablelink" >'+data+'</a>';
		     }
		}, {
			"mDataProp" : "loanBorrower",
			"sTitle" : "贷款人",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "clientCardId",
			"sTitle" : "身份证号",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "payDate",
			"sTitle" : "贷款日期",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				var value = '';
				if(_is_empty(data)){
					value = '';
				}else{
					value = new Date(data).format('yyyy-MM-dd');
				}
				
	            return value;
		     }
		}, {
			"mDataProp" : "capitalTotal",
			"sTitle" : "贷款金额",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "capitalRemain",
			"sTitle" : "剩余本金",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "deadlineStr",
			"sTitle" : "逾期期数",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "overdueDays",
			"sTitle" : "逾期天数",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return (data>0? '<div style="color: red;">'+data+'</div>' :data);
			}
		}, {
			"mDataProp" : "repayEndDate",
			"sTitle" : "还款日期",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				var value = '';
				if(_is_empty(data)){
					value = '';
				}else{
					value = new Date(data).format('yyyy-MM-dd');
				}
				
	            return value;
		     }
		}, {
			"mDataProp" : "loanCollectionName",
			"sTitle" : "催收人",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "collectionAllotStatus",
			"sTitle" : "分配状态",
			"bSortable": true,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return (data==1?'<div style="color: red;">'+(top.SYS_BASE_DATA['Loan_collection_Status_Code'][data]||'')+'</div>' :top.SYS_BASE_DATA['Loan_collection_Status_Code'][data]||'');
			}
		},{
			"mDataProp" : "",
			"sTitle" : "操作",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		      var id = row.id;
		      //var parseStr = '<a href="jsp/loan/loan_repayment_details.jsp?repaymentId='+row.repaymentId+'&loanId='+row.loanId'" class="tablelink">收款明细</a>&nbsp;&nbsp;&nbsp;';
		      var parseStr = '<a href="javascript:void(0);" onclick="queryDetail('+row.repaymentId+','+row.loanId+');" class="tablelink">收款明细</a>&nbsp;&nbsp;&nbsp;';
	    	  if(row.collectionAllotStatus==1){
	    		  //未分配
	    		  parseStr += '<a href="javascript:void(0);" onclick="giveCollection('+row.loanId+');" class="tablelink">催收分配</a>&nbsp;&nbsp;&nbsp;';
	    	  }else if(row.collectionAllotStatus==2){
	    		  parseStr += '<a href="jsp/loan/loan_collection_result_detail.jsp?id='+row.loanId+'" class="tablelink">催收记录</a>&nbsp;&nbsp;&nbsp;';
	    	  }
	    	  return parseStr;
		    }
			
		}],
		"oLanguage": {
            "sUrl":parent.basePath+"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
        }
        
	});
	
	//查询事件
	$('input[name="query_btn"]').click(function(){
		fnDraw();
	});
};

//重新刷新列表数据
var fnDraw = function(){
	$('#riskWarningTable').dataTable().fnDraw();
};

var queryDetail = function(id,loanId){
	parent._do_goto_right(parent.basePath+'jsp/loan/loan_repayment_details.jsp?repaymentId='+id+'&loanId='+loanId);
};

//自定义数据获取函数
var retrieveData = function ( sSource, aoData, fnCallback ) {
		var array = $("#_form_search_default").serializeArray();
		
		aoData = parent._set_query_conditions(aoData,array);
		aoData.push({'name':'v2hldghlcib0agugzgf0ysbhy2nlc3m','value':1});
	        $.ajax( {
	                type: "POST", 
	                url: sSource, 
	                cache : false,
	                dataType : "json",
	                data : aoData, 
	                success: function(resp) {
	                	if(resp && resp.state == 200){
	                		fnCallback(resp.datagrid);
	                		return false;
	                	}
	                	fnCallback(null);
	            }
	        });
};

var contract_query = function(id){
	parent._do_goto_right('jsp/loan/loan_contract_detail.jsp?loanId='+id);
};

//分配催收
var giveCollection = function(id){
	$.dialog({
		id:'giveCollection',
		title:'催收分配',
		lock:true,
		content:'url:jsp/loan/loan_collection_given.jsp',
		data:{'id':id},
		width:400,
		height:400
	});
};

//初始化
$(function(){
	initGrid();
	$('#collectionAllotStatus').mSelect({
		width:100,
		baseCode:'Loan_collection_Status_Code'
	});
});